package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yami.shop.bean.model.ShopChainApply;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ShopChainApplyService;
import com.yami.shop.service.ShopDetailService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: LSR
 * @Date: 2022/12/28 10:00
 */

@RestController
@RequestMapping("/admin/shopChainApply")
public class ShopChainApplyController {
    @Autowired
    private ShopChainApplyService shopChainApplyService;
    @Autowired
    private ShopDetailService shopDetailService;
    @GetMapping("/list")
    @Operation(summary = "店铺链账号配置列表", description = "店铺链账号配置列表")
    public ServerResponseEntity<List<ShopChainApply>> list(ShopChainApply shopChainApply) {
        LambdaQueryWrapper<ShopChainApply> tagQueryWrapper = new LambdaQueryWrapper<>();
        tagQueryWrapper.eq(ShopChainApply::getShopId,SecurityUtils.getShopUser().getShopId()).gt(ShopChainApply::getStatus, 0).orderByDesc(ShopChainApply::getCreateTime);
        List<ShopChainApply> tagList = shopChainApplyService.list(tagQueryWrapper);
        return ServerResponseEntity.success(tagList);
    }

    @PostMapping
    @Operation(summary = "新增", description = "新增")
    public ServerResponseEntity<Void> save(@RequestBody ShopChainApply shopChainApply) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        QueryWrapper<ShopChainApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chain_code", shopChainApply.getChainCode());
        queryWrapper.eq("shop_id", shopId);
        queryWrapper.in("status", 2,1);
        ShopChainApply tempTag = shopChainApplyService.getOne(queryWrapper);
        if (tempTag != null) {
            return ServerResponseEntity.showFailMsg("该链配置申请已经存在");
        }
        ShopDetail shopDetailByShopId = shopDetailService.getShopDetailByShopId(shopId);
        shopChainApply.setShopName(shopDetailByShopId.getShopName());
        shopChainApply.setApplyUserId(SecurityUtils.getShopUser().getUserId());
        shopChainApply.setShopId(shopId);
        shopChainApply.setStatus(1);
        shopChainApply.setCreateTime(new Date());
        shopChainApplyService.save(shopChainApply);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "修改", description = "修改")
    public ServerResponseEntity<Void> update(@RequestBody ShopChainApply shopChainApply) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        QueryWrapper<ShopChainApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chain_code", shopChainApply.getChainCode());
        queryWrapper.ne("id", shopChainApply.getId());
        queryWrapper.eq("shop_id", shopId);
        queryWrapper.eq("status", 2);
        ShopChainApply tempTag = shopChainApplyService.getOne(queryWrapper);
        if (tempTag != null) {
            return ServerResponseEntity.showFailMsg("该链配置已存在");
        }
        shopChainApply.setStatus(1);
        shopChainApply.setUpdateTime(new Date());
        shopChainApplyService.updateById(shopChainApply);
        return ServerResponseEntity.success();
    }
    @DeleteMapping("/{uids}")
    @Operation(summary =  "删除", description = "新增广场分类")
    public ServerResponseEntity<Void> delBatchTag(@PathVariable("uids") Long[] uids){
        List<ShopChainApply> blogs = shopChainApplyService.listByIds(Arrays.asList(uids));
        List<Long> collect1 = blogs.stream().map(a -> a.getId()).collect(Collectors.toList());
        shopChainApplyService.removeBatchByIds(collect1);
        return ServerResponseEntity.success();
    }
}
